var express = require('express');
var router = express.Router();
var url = require("url");
var md5 = require('md5');

var MongoClient = require("./../mongodb/MongoClient");
var MySql = require("./../mongodb/MySql");



router.get('/', function(req, res, next) {
  res.send("<script>window.location.href='http://127.0.0.1/A-Sweetown/%E6%9C%80%E5%90%8E%E4%B8%80%E9%98%B6%E6%AE%B5/project/Personnel%20Management%20System/register.html'</script>");
});

router.get('/reg',function (req,res ,next) {
	var registerObj = url.parse(req.url, true).query;
	registerObj.password = md5(registerObj.password);
//	console.log(registerObj)
	MongoClient.mongoConnect('pull', function(db) {
		console.log('数据库链接成功')
		MySql.findData(db,'reg',registerObj,{},function(result){
//			console.log(result)
			if(result.length == "0"){//表示没有注册
				MySql.insertOneData(db,'reg',registerObj,function(result){
					console.log('插入成功')
					res.send('1');
					db.close();
				})
			}else{//表示已经注册过
				res.send("0");
				db.close();
			}
		})
	},function(err){
		console.log(err);
	})
})
	
module.exports = router;
